package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.model.auto.MajorCategoryInfo;
import com.model.auto.StuMajorRel;
import com.model.auto.req.StuMajorRelsReq;
import com.service.IMajorCategoryInfoService;
import com.service.IStuMajorRelService;
import com.service.IStuSubjectRelService;
import com.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 学生擅长科目关系表 前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2022-09-14
 */
@RestController
@RequestMapping("/stuMajorRel")
public class StuMajorRelController{
    @Autowired
    private IStuMajorRelService stuMajorRelService;
    @Autowired
    private IStuSubjectRelService stuSubjectRelService;
    @Autowired
    private IMajorCategoryInfoService majorCategoryInfoService;


    @GetMapping("/getStuMajor")
    public R getStuMajor(Integer stuId) {
        if (stuId == null) {
            R.error("参数错误，学生id不能为空");
        } else {
            List<StuMajorRel> stuMajorList = stuMajorRelService.list(new LambdaQueryWrapper<StuMajorRel>()
                    .eq(StuMajorRel::getStuId, stuId).isNotNull(StuMajorRel::getMajorCode)
                    .orderByDesc(StuMajorRel::getScore)
            );
            if(Objects.nonNull(stuMajorList)){
                stuMajorList.forEach(stuMajorRel -> {
                    MajorCategoryInfo majorCategoryInfo = majorCategoryInfoService.queryByParam(1,2,stuMajorRel.getMajorCode());
                    if(Objects.nonNull(majorCategoryInfo)){
                        stuMajorRel.setMajorName(majorCategoryInfo.getName());
                        stuMajorRel.setParentMajorName(majorCategoryInfo.getParentName());
                    }
                });
            }
            return R.ok(stuMajorList);
        }
        return R.ok();
    }


    @PostMapping("/preservationLocation")
    public R preservationLocation(@RequestBody StuMajorRelsReq stuMajorRel) {
        System.out.println("map = " + stuMajorRel);
        stuMajorRelService.saveOrUpdateBatch(stuMajorRel.getStuMajorRel());
        stuSubjectRelService.saveOrUpdateBatch(stuMajorRel.getStuSubjectRel());
        return R.ok();
    }


}
